package org.jivesoftware.spark.crowd.packet;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.crowd.Param;
import org.xmlpull.v1.XmlPullParser;

public class DiscoverCrowds extends IQ {
	private final List<CrowdItem> items = new CopyOnWriteArrayList<CrowdItem>();
	
	private String xmlns = "";

	@Override
	public String getChildElementXML() {
		StringBuilder buf = new StringBuilder();
		buf.append("<crowd xmlns=\"").append(xmlns)
				.append("\"");
		
		buf.append(">");
		synchronized (items) {
			for (CrowdItem item : items) {
				buf.append(item.toXML());
			}
		}
		buf.append("</crowd>");
		return buf.toString();
	}

	

	public void addItem(CrowdItem item) {
		synchronized (items) {
			items.add(item);
		}
	}

	public Iterator<DiscoverCrowds.CrowdItem> getItems() {
		synchronized (items) {
			return Collections.unmodifiableList(items).iterator();
		}
	}
	
	
	public String getXmlns() {
		return this.xmlns;
	}



	public void setXmlns(String xmlns) {
		this.xmlns = xmlns;
	}


	public static class CrowdItem {

		private String crowdJID;
		private String name;
		private String creatorJID;
		private String creatorNickName;
		private String createTime;
		private String title;
		
		public String getCrowdJID() {
			return crowdJID;
		}

		public void setCrowdJID(String crowdJID) {
			this.crowdJID = crowdJID;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getCreatorJID() {
			return creatorJID;
		}

		public void setCreatorJID(String creatorJID) {
			this.creatorJID = creatorJID;
		}

		public String getCreateTime() {
			return createTime;
		}

		public void setCreateTime(String createTime) {
			this.createTime = createTime;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}
		public String getCreatorNickName() {
			// TODO Auto-generated method stub
			return creatorNickName;
		}
		public void setCreatorNickName(String creatorNickName2) {
			this.creatorNickName = creatorNickName2;
			
		}
		public String toXML() {
			StringBuilder buf = new StringBuilder();
			buf.append("<crowdItem crowdJID=\"").append(crowdJID).append("\"");
			if (name != null) {
				buf.append(" name=\"").append(name).append("\"");
			}
			if (creatorJID != null) {
				buf.append(" creatorJID=\"").append(creatorJID).append("\"");
			}
			if(creatorNickName != null){
				buf.append(" creatorNickName=\"").append(creatorNickName).append("\"");
			}
			if (createTime != null) {
				buf.append(" createTime=\"").append(createTime).append("\"");
			}
			if (title != null) {
				buf.append(" title=\"").append(title).append("\"");
			}
			buf.append("/>");
			return buf.toString();
		}

		

		
	}
	
	public static class DiscoverCrowdsProvider implements IQProvider{

		private static DiscoverCrowdsProvider instance;
		
		public static DiscoverCrowdsProvider getInstance(){
			if(instance == null)
				instance = new DiscoverCrowdsProvider();
			return instance;
		}
		private DiscoverCrowdsProvider(){
			ProviderManager.getInstance().addIQProvider("crowd", Param.discoLocalCrowds_NS, this);
			/*
			 *search also use this because they have the same structure.
			 */
			ProviderManager.getInstance().addIQProvider("crowd", Param.search_NS, this);
		}
		@Override
		public IQ parseIQ(XmlPullParser parser) throws Exception {
			DiscoverCrowds discoverCrowds = new DiscoverCrowds();
//			String xmlns = parser.getAttributeValue("", "xmlns");
			String xmlns = parser.getNamespace();
			
			discoverCrowds.setXmlns(xmlns);
			boolean done = false;
			while(!done){
				 int eventType = parser.next();
				 
				 if(eventType == XmlPullParser.START_TAG){
					 if(parser.getName().equals("crowdItem")){
						 String crowdJID = parser.getAttributeValue("", "crowdJID");
						 String name = parser.getAttributeValue("","name");
						 String creatorJID = parser.getAttributeValue("", "creatorJID");
						 String createTime = parser.getAttributeValue("", "createTime");
						 String title = parser.getAttributeValue("", "title");
						 String creatorNickName = parser.getAttributeValue("", "creatorNickName");
						 DiscoverCrowds.CrowdItem item = new DiscoverCrowds.CrowdItem();
						 
						 item.setCrowdJID(crowdJID);
						 item.setName(name);
						 item.setCreatorJID(creatorJID);
						 item.setCreateTime(createTime);
						 item.setTitle(title);
						 item.setCreatorNickName(creatorNickName);
						 discoverCrowds.addItem(item);
					 }
				 }else if(eventType == XmlPullParser.END_TAG){
					 if(parser.getName().equals("crowd"))
						 done = true;
				 }
			}
			return discoverCrowds;
		}
		
	}

}
